package com.zzuli.xyd.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.jdbc.result.UpdatableResultSet;
import com.zzuli.xyd.mapper.UserMapper;
import com.zzuli.xyd.pojo.Userinfo;
import com.zzuli.xyd.service.IUserService;
import com.zzuli.xyd.util.MDigest5;
import com.zzuli.xyd.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @auther pony
 * @create 2022-07-06 10:22
 */
@Service
public class UserService implements IUserService {
    //到spring容器取对象
    @Autowired
    private UserMapper userMapper;

    @Override
    public Userinfo ckUsername(String username) {
        //设置查询条件
        QueryWrapper<Userinfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public Boolean addUser(Userinfo userinfo) {
        int insert = userMapper.insert(userinfo);
        return insert == 1 ? true : false;
    }

    @Override
    public Boolean rePwd(Userinfo userinfo) {
        UpdateWrapper<Userinfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("username", userinfo.getUsername());
        updateWrapper.set("userpwd", userinfo.getUserpwd());
        int update = userMapper.update(userinfo, updateWrapper);
        return update == 1 ? true : false;
    }

    /**
     * 根据id查找数据
     *
     * @param id
     * @return
     */
    @Override
    public Userinfo findById(Integer id) {
        return userMapper.selectById(id);
    }

    /**
     * 根据id修改记录
     *
     * @param model
     * @return
     */
    @Override
    public int update(Userinfo model) {
        return userMapper.updateById(model);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    public int deleteBatch(String[] ids) {
        return userMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public PageUtil<Userinfo> getUserinfoPage(Integer pageIndex, Integer pageSize, String keyword){
        //判断每页记录数有效性，如果小于1，设置5记录
        pageSize = pageSize==null || pageSize < 1 ? 5:pageSize;
        PageUtil<Userinfo> pageUtil = new PageUtil<>();
        //判断关键字的有效性
        keyword = "null".equals(keyword) || "".equals(keyword) ? null : keyword;
        pageUtil.setPageSize(pageSize);
        //设置总记录数
        pageUtil.setTotal(userMapper.getTotal(keyword));
        //查询到数据
        if(pageUtil.getTotal()>0){
            // 判断pageIndex数据有效性
            pageIndex = pageIndex==null || pageIndex <0 ? 0:pageIndex;  // 异常数据(负数,null)
            pageIndex = pageIndex >  pageUtil.getPages() ? pageUtil.getPages():pageIndex; // 如果pageIndex的值大于总页数
            // 设置页面索引
            pageUtil.setPageIndex(pageIndex);
            List<Userinfo> list = userMapper.getUserinfoPage((pageIndex-1)*pageSize, pageSize, keyword);
            pageUtil.setRecords(list);
        }
        return pageUtil;
}
}
